var wechatMsgHandler = require('../service/wechatMsgHandler');
var wechatAccessToken = require('../service/wechatAccessToken');
var Suggest = require('../proxy').Suggest;
var logger = require('../service/Logger');
var WechatPay = require('../lib/WechatPay');
var WechatAPI = require('wechat-api');

/**
 * 测试连接
 * @param req
 * @param res
 * @param next
 */
exports.wechatGet = function(req, res, next){
    var signature = req.query.signature;
    var timestamp = req.query.timestamp;
    var nonce = req.query.nonce;
    var echostr = req.query.echostr;
    console.log('signature='+signature);
    console.log('timestamp='+timestamp);
    console.log('nonce='+nonce);
    console.log('echostr='+echostr);
    res.send(echostr);
}
/**
 * 接受消息
 * @param req
 * @param res
 * @param next
 */
exports.wechatPost = function(req, res, next){
    wechatMsgHandler.messageRouter(req, res, next);
}

exports.setCookie = function(req, res, next){
    var openId = req.query.openId;
    res.cookie('fj_wechat', openId, {maxAge:6000000*24, httpOnly:true, path:'/', secure:false});
    res.send('Set Cookies :'+openId);
}

exports.deletCoockie = function(req, res, next){
    res.cookie('fj_wechat', '', {maxAge:6000000*24, httpOnly:true, path:'/', secure:false});
    res.send('Delete Cookie : ' + req.cookies.fjoy_wechat);
}
/**
 * 得到acctoken
 * @param req
 * @param res
 */
exports.getAccesstoken = function(req, res, next){
    wechatAccessToken.getWechatAccToken(function(err, token){
        if(err) return res.send({error: err.message});
        res.send(token)
    })
}
/**
 * 意见反馈
 * @param req
 * @param res
 * @param next
 */
exports.sentSuggest = function(req, res, next){
    var content = req.body.content,
        me = req.params.currentUser;
    Suggest.newAndSave(me, content, function(err, data){
        if(err) return logger.error(err.message);
        res.send({data: data})
    })
}
/**
 * 获得jsapi 签名信息
 * @param req
 * @param res
 */
exports.jsapiSign = function(req, res){
    var url = req.body.url;
    var signInfo = WechatPay.jsapiSign(url, limitStackMap['jsapi_ticket']);
    res.send({signInfo: signInfo})
}

exports.toError = function(req, res, next){

    res.render('others/error', {});
}

exports.dashang = function(req, res, next){
    req.params.currentUser = {};
    res.render('dashang/dashang', {});
}